package com.navi168.fwq21.hyzl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import com.navi168.fwq21.R;
import com.navi168.fwq21.db.Sqlh;
import com.navi168.fwq21.friend.mod.Myfriend;
import com.navi168.fwq21.util.SendPosition;

import android.R.string;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class SendList extends Activity {
	private Button newfriend;
	private Button okButton;
	private Button noButton;
	private TextView shownum;
	private Sqlh sqlh;
	private ArrayList<Myfriend> myfriends = new ArrayList<Myfriend>();
	private Context context;
	private MyAdapter mAdapter;
	private ListView friendsListView;
	private String[] number_arry = new String[2];
	private HashMap<String, String> numbermap = new HashMap<String, String>();// 接受的号码
	private HashMap<String, String> namemap = new HashMap<String, String>();
	private String twonumber;
	private int isenable = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.friendlist);
		findViews();
		setlisters();
		sqlh = sqlh.getInstance(SendList.this);

		myfriends = sqlh.getMyfriends();
		context = getApplicationContext();
		mAdapter = new MyAdapter();
		friendsListView.setAdapter(mAdapter);
	}

	private void showToast(String showString) {
		Toast.makeText(getApplicationContext(), showString, Toast.LENGTH_SHORT)
				.show();
	}

	private void setlisters() {
		newfriend.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				showDialog(0);

			}
		});
		okButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Bundle bundle = SendList.this.getIntent().getBundleExtra(
						"poi_xf");
				
				if (numbermap.size() < 1) {
					showToast("请至少选择一个号码？");
				} else {
					bundle.putString("postnum", getNumberText(numbermap));
					bundle.putSerializable("postname", getNumberText(namemap));
					Intent intent = new Intent(SendList.this,
							SendPosition.class);
					intent.putExtra("sendlist", bundle);
					startActivity(intent);
				}
			}
		});
		noButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				SendList.this.finish();
			}
		});

	}

	private void findViews() {

		newfriend = (Button) findViewById(R.id.addnewfriend);
		friendsListView = (ListView) findViewById(R.id.myfriendlist_id);
		shownum = (TextView) findViewById(R.id.shownumbertext_c);
		okButton = (Button) findViewById(R.id.myfriend_ok);
		noButton = (Button) findViewById(R.id.myfriend_cance);

	}

	@SuppressWarnings({ "unused", "rawtypes" })
	private String getNumberText(HashMap<String, String> nums) {
		Iterator<Entry<String, String>> iter = nums.entrySet().iterator();
		String newnum = "";
		while (iter.hasNext()) {
			Map.Entry entry = (Map.Entry) iter.next();
			newnum += entry.getValue() + " ";
		}

		return newnum;

	}

	@Override
	protected Dialog onCreateDialog(int id) {
		AlertDialog.Builder builder = new Builder(this);

		if (id == 0) {

			LayoutInflater inflater = LayoutInflater.from(this);
			// 获取相关的布局
			final View layout = inflater
					.inflate(R.layout.newfrienddialog, null);

			builder.setView(layout);
			builder.setTitle("增加一个新的好友！");
			builder.setPositiveButton("保存",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							EditText friendname = (EditText) layout
									.findViewById(R.id.friendname);
							EditText friendmob = (EditText) layout
									.findViewById(R.id.friendmob);
//							EditText friendcar = (EditText) layout
//									.findViewById(R.id.friendcar);
							EditText friendemail = (EditText) layout
									.findViewById(R.id.friendemail);
							Myfriend mf = new Myfriend();
							mf.setFriendemail(friendemail.getText().toString()
									.trim());
//							mf.setFriendcar(friendcar.getText().toString()
//									.trim());
							mf.setFriendname(friendname.getText().toString()
									.trim());
							mf.setFriendmob(friendmob.getText().toString()
									.trim());
							int flag = sqlh.add_Newmyfriend(mf);
							if (flag > 0) {
								showToast("增加新用户成功！");
							} else {
								showToast("增加新用户失败！");
							}
							myfriends = sqlh.getMyfriends();
							mAdapter.notifyDataSetChanged();
							dialog.dismiss();

						}
					});
			builder.setNegativeButton("取消",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();

						}
					});

		}
		return builder.create();

	}

	public class MyAdapter extends BaseAdapter {
		ViewHolder vHolder = null;

		@Override
		public int getCount() {
			//
			return myfriends.size();
		}

		@Override
		public Object getItem(int arg0) {
			//
			return myfriends.get(arg0);
		}

		@Override
		public long getItemId(int position) {
			//
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {

			if (convertView == null) {
				vHolder = new ViewHolder();
				LayoutInflater inflater = (LayoutInflater) context
						.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = inflater.inflate(R.layout.myfriend_c, parent,
						false);
				vHolder.fname = (TextView) convertView
						.findViewById(R.id.friendname);
				vHolder.fstate = (TextView) convertView
						.findViewById(R.id.friendstate);
				vHolder.selected = (CheckBox) convertView
						.findViewById(R.id.ischecked);
				convertView.setTag(vHolder);

			} else {
				vHolder = (ViewHolder) convertView.getTag();
			}

			vHolder.fname.setText(myfriends.get(position).getFriendname());
			if (myfriends.get(position).getFriendstate() == "1") {
				vHolder.fstate.setText("在线");
			} else {
				vHolder.fstate.setText("离线");
			}
			vHolder.selected.setChecked(false);
			vHolder.selected
					.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

						@Override
						public void onCheckedChanged(
								final CompoundButton buttonView,

								boolean isChecked) {

							if (isChecked) {
								if (isenable > 1) {
									buttonView.setChecked(false);
								} else {
									isenable += 1;
									number_arry[0] = myfriends.get(position)
											.getFriendmob() == null ? "无此会员手机号"
											: myfriends.get(position)
													.getFriendmob();
									number_arry[1] = myfriends.get(position)
											.getFriendcar() == null ? "无此会员车机号"
											: myfriends.get(position)
													.getFriendcar();

									AlertDialog.Builder builder = new Builder(
											SendList.this);
									builder.setTitle("请选择号码");

									builder.setItems(
											number_arry,
											new DialogInterface.OnClickListener() {

												public void onClick(
														DialogInterface dialog,
														int which) {

													if (!number_arry[which]
															.contains("无")) {
														
														numbermap.put(
																String.valueOf(position),
																number_arry[which]);
														namemap.put(String.valueOf(position), myfriends.get(position).getFriendname());

														shownum.setText(getNumberText(numbermap));

													} else {
														buttonView
																.setChecked(false);
													}

												}

											});
									builder.create().show();

								}
								Log.i("mylog", isenable + "isenable");
							} else {

								isenable -= 1;
								numbermap.remove(String.valueOf(position));
								namemap.remove(String.valueOf(position));
								shownum.setText(getNumberText(numbermap));

							}

						}

					});

			return convertView;
		}

	}

	public class ViewHolder {
		public CheckBox selected;
		public TextView fname;
		public TextView fstate;
	}

}
